<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
     <script type="text/javascript">
        function passwordMsg(passTxt , passTxt2){
	        var mb = {};
	        mb.isOk = true;
	        mb.msg = true;
	        if(passTxt == null || passTxt == ''){
	           mb.msg = '密码不能为空';
	           mb.isOk = false;
	        }else if(passTxt.length <6){
	           mb.msg = '密码最少长度为6位';
	           mb.isOk = false;
	        }else if(passTxt.length >25){
	           mb.msg = '密码最长长度为25位';
	           mb.isOk = false;
	        }else{
		       if(typeof(passTxt2)!='undefined'){
		           if(passTxt!=passTxt2){
		             mb.msg = '二次输入的密码不一致';
		             mb.isOk = false;
		           }
		       }
	        }
	        
	        
	        return mb;
	     }
     
        function register_saveUser(){
           var userName = $("#userName").val();
           var userPass = $("#userPass").val();
           var reEnterPwd = $("#reEnterPwd").val();
           var userid = '<s:property value="user.id"/>';
           
           if(userName == ''){
              alert("用户名不能为空");
              return;
           }
           
            if(userName!="" && !isEmail(userName)){
        	  alert("用户名格式为邮件格式(如:abc@163.com)");
        	  return;
           }
           
           if(userPass != '' || reEnterPwd !=''){
             var obj = passwordMsg(userPass , reEnterPwd);
             if(obj.isOk == false){
                alert(obj.msg);
                return;
             }
           }
           
           var url = "cts/findUserByType?username="+userName+"&type=1&returnUid=0";
           $.post(url , "" , function(data){
              if(data!=userid && data!=0){
                  alert("此用户名已存在");
              }else{
                   var param = $("#userForm").serialize();
		           $.post("cts/saveUser.action" , param , function(data){
		               if(data == 1 || data == 2){
		                 alert("用户更新成功");
		                 window.location.href = "${pageContext.request.contextPath}/cts/index";
		               }else{
		                 alert("用户更新失败");
		               }
		           });
              }
           });
        }
        
         function isEmail(str) {
			if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
				return true;
			} else {
				return false;
		    }
		}
		
		
		  // 个人近照
        function showUpLoadPage(obj){
		     if(!(document.cookie || navigator.cookieEnabled)){
		        alert("请开启cookie");
		        return;
		     }
		     
		     var randomnumber = Math.floor(Math.random() * 100000);
		     $("#uploadImage").find("iframe[id='headIframeI']").remove();
		     $("#uploadImage").find("div[class='popdiv_content']").html('<iframe id="headIframeI" src="javascript:void(0)" style="width:317px;height:420px;margin:0px;padding:0px;" frameborder="0" scrolling="no"></iframe>'); 
			 $("#uploadImage").find("iframe[id='headIframeI']").attr("src" , "toImageTool?uptype=1&ishead=true&randomnumber="+randomnumber);  
		     $("#uploadImage").find("div[class='popdiv_handle']").text("用户头像");
		     openPopDiv( 'uploadImage' , '' ,  200); 
	   }
	   
	   function imgReturnVal(isTimeFile , fileBookName , imgUrl , imgName){
	      var randomnumber = Math.floor(Math.random() * 100000);
	      imgUrl = '${pageContext.request.contextPath}/' + imgUrl+"?time="+randomnumber
	      $("#imgHead").attr("src" , imgUrl);
	      $("#userLogth").val(imgName);
	      $("#isTimeUp").val(isTimeFile);
	      $("#timeFileName").val(fileBookName);
	      
	      $("#uploadImage").hide();
	   }
     </script>
     
     <style type="text/css">
       .inputwidth{
         width:150px;
       }
     </style>
  </head>
  
  <body>
	    <form action="cts/saveUser.action" id="userForm" name="n_userForm">
	         <input type="hidden" id="userId" name="user.id" value="<s:property value="user.id"/>"/>
             <input type="hidden" id="status" name="user.status" value="<s:property value="user.status"/>"/>
             <input type="hidden" id="userLogth" name="user.logoPath" value="<s:property value="user.logoPath"/>"/>
             
             <input type="hidden" id="createTime" name="user.createTime" value="<s:property value="user.createTime"/>"/>
             <input type="hidden" id="isupass" name="isupass" value="0"/>
             <input type="hidden" id="isTimeUp" name="isTimeUp" value=""/>
             <input type="hidden" id="timeFileName" name="timeFileName" value=""/>
             <input type="hidden" id="timePasswd" name="timePasswd" value="<s:property value="user.userPass"/>"/>
		    
		     <table>
		       <tr>
		          <td rowspan="5" valign="top" title="上传头像">
		            <div style="width:40px;height:40px;background-color:#ececec;" onclick="showUpLoadPage()">
			             <s:if test="user == null || user.logoPath == '' || user.logoPath == null">
			               <img id="imgHead" src="${pageContext.request.contextPath}/cts/images/original.jpg" style="width:40px;height:40px;margin-top:4px;cursor:pointer;" title="点击上传用户头像"/>
			             </s:if>
			             <s:else>
			               <img id="imgHead"  src="${pageContext.request.contextPath}/upload/user/<s:property value="user.id"/>/<s:property value="user.logoPath"/>"  style="width:40px;height:40px;margin-top:4px;cursor:pointer;" title="点击上传用户头像"/>
			             </s:else>
		             </div>
		          </td>
		          <td></td>
		       </tr>
		       <tr>
		         <td align="right">用户名：</td>
		         <td align="left">
			          <input class="inputwidth" type="text" id="userName" name="user.userName" value="<s:property value="user.userName"/>"/>
		         </td>
		       </tr>
		       <tr>
		         <td align="right">密码：</td>
		         <td><input class="inputwidth"  type="password" id="userPass" name="user.userPass"/></td>
		       </tr>
		       <tr>
		         <td align="right">确认密码：</td>
		         <td><input class="inputwidth"  type="password" id="reEnterPwd" name="reEnterPwd"/></td>
		       </tr>
		       <tr>
		         <td colspan="2">
		            <input type="button" id="enterSave" name="enterSave" value="确认" onclick="register_saveUser()"/>
		            <input type="reset" id="reset" name="reset" value="重置"/>
		         </td>
		       </tr>
		     </table>
	    </form>
  </body>
</html>
